package com.mach.platform.service;

import com.mach.platform.domain.OrderData;
import com.mach.platform.repository.OrderDataRepo;
import com.mach.platform.utils.BeanChangeUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class OrderDataServiceImpl implements BaseService<OrderData>  {
    @Autowired
    private OrderDataRepo orderDataRepo;
    @Autowired
    private OrderServiceImpl orderService;
    @Override
    public OrderData save(OrderData orderData) {
        OrderData PLCOrderData = orderService.getOrderData();
        //设备时间
        PLCOrderData.setCreateTime(LocalDateTime.now());
        //合并两个实体
        BeanChangeUtil<T> beanChangeUtil = new BeanChangeUtil<>();
        OrderData newOrderData = (OrderData) beanChangeUtil.combineSydwCore(PLCOrderData, orderData);
        return orderDataRepo.save(newOrderData);
    }

    @Override
    public List<OrderData> insertObjInBatch(List<OrderData> list) {
        return null;
    }

    @Override
    public OrderData update(OrderData orderData) {
        return null;
    }

    @Override
    public int delete(Long[] ids) {
        return 0;
    }

    @Override
    public Page<OrderData> list(Pageable pageable, Map params) {
        return null;
    }

    @Override
    public OrderData info(Long id) {
        return null;
    }

    public OrderData getDataByOrderId(Long id) {
        List<? extends Object> orderDatas = orderDataRepo.getDataByOrderId(id);
        BeanChangeUtil<T> beanChangeUtil = new BeanChangeUtil<>();
        Map<String, Object> objectMap = beanChangeUtil.mergeObj((List<Object>) orderDatas);
        OrderData orderData = (OrderData) beanChangeUtil.mapToBean(objectMap, OrderData.class);
        return orderData;
    }
}
